Repository navigation
Download databases with the physionet package - #659
Conversation
dl_database and dl_files now download files with physionet.download(), from physionet 0.7.0, which takes the exact files to download and where to save each one (files=). wfdb still chooses the files, from RECORDS, ANNOTATORS and each record's header, and saves them in the same layout as before: relative to the database directory (or its subdirectory), or all in dl_dir with keep_subdirs=False. Downloads run two at a time, as before, without progress bars or messages from physionet. Each file is now checked against the database's checksum list. A file that is already saved with the right content is skipped, a partly downloaded one is completed, and any other saved file is downloaded again, instead of comparing only sizes. overwrite=True removes the saved files first. Restricted databases can be downloaded after `physionet login`, and the RECORDS and ANNOTATORS files are read through physionet:// too. dl_database finds which annotation files there are from the checksum list, instead of requesting each one: downloading three MIT-BIH records with their annotations takes 20 requests and 4.9 s instead of 38 requests and 11.2 s. dl_files no longer checks the database's web page first, since physionet reports a missing database, version or file before downloading anything. With a database index URL set by set_db_index_url, files are downloaded from that server as before.
|
Reviewed
Please validate the requested paths and their resolved destinations before removing anything. Ideally, let The 496 offline tests and all PR checks pass; they do not cover this case. |
With overwrite=True, _download_files removed the saved copies before physionet.download checked the requested paths, so a path such as "../victim.dat", or one through a symbolic link inside dl_dir, removed a file outside dl_dir, and then raised ValueError. Before removing anything, check that every file would be saved inside dl_dir, as a plain relative path and after following symbolic links, and that the database has every requested file. If not, raise ValueError or FileNotFoundError without removing any file.
|
Thanks, that was a real problem. Fixed in the latest commit. With
Only then are the saved copies removed and I kept this in wfdb rather than adding forced replacement to New tests, each checking that no file is removed and |
|
Re-reviewed With Please check that resolved destinations are distinct before removing saved files, including names that differ only by case or resolve through a symlink. The 24 focused tests pass; CI was still running when I checked. |
With keep_subdirs=False, two files in different subdirectories, such as sub1/100.hea and sub2/100.hea, are saved under one name. With overwrite=True, _download_files removed the saved file before physionet.download refused the clash. _check_files_to_replace now also checks that no two files would be saved as one before anything is removed: after following symbolic links, and ignoring case, since names that differ only in case are one file on macOS and Windows.
|
Thanks, fixed in the latest commit. Before removing anything for New MIT-LCP/physionet#221 adds |
physionet 0.7.1 adds overwrite to download(), applied after all of its checks of the requested files and destinations, and replacing each saved file only once its new copy is complete and its checksum matches. Pass overwrite through to it, and remove wfdb's own removal of saved files and the checks that had to come before it (_check_files_to_replace). This also keeps a saved file when its new download fails, which removing it first did not. The tests now check that overwrite is passed through and that wfdb removes nothing itself, and run a path outside dl_dir, two files with one name and names that differ only in case through the real physionet.download, which refuses them before replacing anything.
|
physionet 0.7.1 is released, with
The tests now check that |
|
Re-reviewed One documentation correction remains. The |
|
Thanks, fixed in the latest commit. The |
## Summary Bump the version from 5.0.0 to 5.1.0, and collect the changelog entries in `changelog.d/` into `docs/changes.rst` with `towncrier build --date "Nov 2026"`, with a short summary at the top of the new section. This is a minor release: it adds `physionet://` URIs and reads `pn_dir` records and downloads databases through the `physionet` package, which becomes a dependency (#657, #658, #659), speeds up `xqrs_detect` and `gqrs_detect` (#655, #656), and fixes `rdann` and `Annotation.apply_range` (#654, #660, #661). The one change existing code may notice is that `rdann` now raises a `ValueError` when `sampto` is not greater than `sampfrom`, including `sampto=0`, which used to return every annotation. The summary calls it out. It also raises the `physionet` requirement from 0.7.1 to 0.7.2, which retries transient server errors when looking up a database's latest version, as `pn_dir` without a version does, and updates that changelog entry to match. ## Checks - `uv run pytest -m "not network"`: 511 passed, also with `--python 3.11 --resolution lowest-direct` (physionet 0.7.2). `uv run pytest -m network`: 18 passed. - `ruff format --check`, `ruff check` and the Sphinx HTML build with warnings as errors pass. - The wheel and source distribution build, pass `twine check`, and have `Version: 5.1.0` and `Requires-Dist: physionet>=0.7.2`. - PyPI has no 5.1.0 release yet. <sub>@tompollard 🟥 red 🦥 sloth</sub>
Follows #657 and #658.
dl_databaseanddl_filesnow download files withphysionet.download(), using thefiles=andverbose=arguments added in physionet 0.7.0 (MIT-LCP/physionet#218) andoverwrite=added in 0.7.1 (MIT-LCP/physionet#221), so they keep their arguments and folder layout.Downloading the same files on
mainand on this branch against PhysioNet gave identical files (13, including a file in a subfolder and thekeep_subdirs=Falselayout):maindl_database("mitdb", records=["100", "101", "102"], annotators=["atr"])dl_files("mitdb", [...])with 2 filesdl_databaseis faster mainly because it finds which annotation files there are from the checksum list, rather than requesting each one. A smalldl_filescall costs a little more, sincephysionet.download()looks up the version, the checksum list and the source first.Changes
download._download_files(db_dir, dl_dir, files, keep_subdirs, overwrite)maps each file (relative todb_dir, which may include a subdirectory) to where it is saved (the same path relative todl_dir, or its name withkeep_subdirs=False), and callsphysionet.download(slug, version, files=..., verbose=False, show_progress=False, jobs=2, overwrite=overwrite). Two files at a time, no progress bars and wfdb's own messages, as before.overwrite=True, physionet downloads every file again, but only after checking all the requested files and destinations, and replaces each saved file only once its new copy is complete and checked, so wfdb removes nothing itself.physionet login.get_record_listandget_annotatorsreadRECORDSandANNOTATORSwith_open_pn_file, so throughphysionet://too.dl_databasefinds annotation files with a newdownload._project_files(db_dir), which lists the files from the checksum list through thephysionet://filesystem, instead of a request for each record and annotator.dl_filesno longer requests the database's web page first: physionet reports a missing database, version or file before downloading anything. Before, the files listed before a missing one were downloaded first.keep_subdirs=False, two files with the same name now raise aValueErrorfrom physionet, instead of one silently replacing the other.set_db_index_url, both functions download from that server with the existing code.physionet >= 0.7.1is now required. Theoverwritedocs and a new Notes section describe the behaviour, and a changelog entry is added.Testing
tests/test_physionet_uri.pybuild a database in a temporaryphysionetcache (RECORDS,ANNOTATORS, a single-segment, a multi-segment and a subdirectory record, and a checksum list), make every request fail, and replacephysionet.downloadto record its arguments. They check the files and where they are saved, a subdirectory indb_dir,keep_subdirs=False, thatoverwriteis passed through and wfdb removes nothing itself,dl_fileswith both layouts and without a web page request, anddl_fileswithset_db_index_url. Tests ofoverwrite=Truewith a path outsidedl_dir, two files with one name, and names that differ only in case run through the realphysionet.download, which refuses them before replacing anything.uv run pytest -m "not network": 507 passed, also with--python 3.11 --resolution lowest-direct.uv run pytest -m network: 18 passed, including the existingdl_databasetests that download from PhysioNet'safdb.ruff format --check,ruff checkand the docs build pass.@tompollard 🟥 red 🦥 sloth